/**
 * 
 */
package com.uit.project.bustrip.bo;

import org.json.JSONObject;
import android.content.Context;
import android.util.Log;
import com.uit.project.bustrip.appconfig.AppsConfig;
import com.uit.project.bustrip.bointerface.TicketBO;
import com.uit.project.bustrip.entity.Ticket;
import com.uit.project.bustrip.ultil.JSONUltil;
import com.uit.project.bustrip.valueobject.JSONResult;

/**
 * @author Nguyen
 * 
 */
public class TicketBOImpl extends BaseBOImpl<Ticket, Integer> implements TicketBO {
	public TicketBOImpl() {
		super();
	}

	public TicketBOImpl(Context context) {
		super(context);
	}




	private String getOrderURL(Ticket ticket) {
		return AppsConfig.getServiceURL()
				+ "?_module=Ticket&_view=json&_action=order&"
				+ ticket.getURLQueryString();
	}

	@Override
	public JSONResult order(Ticket ticket) {
		String url = getOrderURL(ticket);
		JSONObject jobject = JSONUltil.getJSONObjectFromURL(url);
		if (jobject != null) {
			JSONResult jsonResult = new JSONResult();
			try {
				JSONObject jValue = jobject.getJSONObject(AppsConfig.JSON_OBJECT);
				jsonResult.setMessage(jValue.getString(AppsConfig.JSON_MESSAGE));
				jsonResult.setResult(jValue.getString(AppsConfig.JSON_RESULT));
				if(jsonResult.getResult().equals(AppsConfig.RESULT_SUCCESS)){
					jsonResult.setData(jValue.getString(AppsConfig.JSON_DATA));
				}
				else{
					jsonResult.setData(null);
				}
				return jsonResult;
			} catch (Exception e) {
				Log.e("JSONResult order", e.getMessage());
				return null;
			}
		}
		return null;
	}

}
